MainView.list = {
	data: [],
	doubleClickCopy: function(obj,msg) {
		if(typeof msg == "undefined"){
			msg = "游戏id已经复制";
		}else{
			msg = msg+"已经复制";
		}
		BC_Tool.copyValue($(obj).html(), function() {
			AlertView.layer.msg(msg);
		});
	},
	jiesuan: function(index) {
		AlertView.layer.loading("正在结算(大约1分钟,请勿关闭)...");
		var obj = MainView.list.data[index];
		BC_POST.canDo({
			gameId: obj.gameId,
			player: App.accounts[0],
			action: "settle"
		}).then(function(result) {
			BC.do("checkAndSettle", {
				hashId: "0x"+obj.gameId
			}).then(function(result) {
				console.log(result);
				$(".layui-layer  .notify_loading .content1 .title").html("正在等待合约执行完成...");
				$(".layui-layer .notify_loading .content1 ").css("background-color","lightgray");
				BC_WS.addObserver("state", function(data, p) {
					$(".layui-layer .notify_loading .content1 ").css("background-color","white");
					console.log("MainView addObserver state" + data);
					if(data.state == "5" && obj.gameId == data.gameId) {
						AlertView.layer.alert("成功", "结算成功。");
						MainView.list.search({});
						return false;
					} else {
						return true;
					}
				}, true);
				
				
			}).catch(function(err) {
				AlertView.layer.alert("失败", err.msg);
			});
		}).catch(function(err) {
			AlertView.layer.alert("失败", err.msg);
		});
	},
	challenge: function(index) {
		var obj = MainView.list.data[index];
		
		AlertView.layer.loading("正在向对方发起挑战...");
		BC_WS.talk1({
			player: App.accounts[0],
			gameId: obj.gameId,
			cmd: "talk1"
		}).
		then(function(data1) {
			BC.getBalance().then(function(data) {
				if(data.toNumber() > obj.stake) {
					MainView.apply.applyBtnClick({
						gameId: obj.gameId,
						stake: obj.stake
					});
				} else {
					AlertView.layer.alert("挑战失败", "您的账户余额不足");
				}
			}).catch(function(err) {
				console.log(err);
				AlertView.layer.alert("失败", err.msg);
			});
		}).catch(function(err) {
			console.log(err);
			AlertView.layer.alert("失败", err.msg);
		});
	},
	destroy: function(index) {
		var obj = MainView.list.data[index];
		$(".layui-layer .notify_loading .content1 ").css("background-color","white");
		AlertView.layer.loading("正在撤销(大约1分钟,请勿关闭)...");
		
		BC_POST.canDo({
			gameId: obj.gameId,
			player: App.accounts[0],
			action: "destroy"
		}).then(function(result) {
			BC.do("destroy", {
				gameId: "0x" + obj.gameId
			}).then(function(result) {
				console.log(result);
				$(".layui-layer  .notify_loading .content1 .title").html("正在等待合约执行完成...");
				$(".layui-layer .notify_loading .content1 ").css("background-color","lightgray");
				
				BC_WS.addObserver("state", function(data, p) {
					$(".layui-layer .notify_loading .content1 ").css("background-color","white");
					console.log("MainView addObserver state" + data);
					if(data.state == "5" && obj.gameId == data.gameId) {
						AlertView.layer.alert("成功", "撤销成功");
						MainView.list.search({});
						return false;
					} else {
						return true;
					}
				}, true);
			}).catch(function(err) {
				AlertView.layer.alert("撤销失败", err.msg);
			});
		}).catch(function(err) {
			AlertView.layer.alert("撤销失败", err.msg);
		});
	},
	init: function() {
		$("hashValue").on("dbClick", this.doubleClickCopy)
	},
	search: function(param) {
		console.log("####查询");
		if(typeof param == "undefined") {
			param = {};
			var searchId = $("#searchValue").val();
			var state = $("#searchState").val();
			if(searchId != "") {
				param.searchId = searchId;
			}
			if(state != "") {
				param.state = state;
			}
		}
		BC_POST.search(param).then(function(data) {
			console.log(data);
			for(var i in data.data) {
				data.data[i].account = App.accounts[0];
			}
			$(".listTemp").remove();
			var html = template("art-listTemplate", data);
			$("table.mainList tbody").append(html);
			MainView.list.data = data.data;
			var xx = $(".hashId");
			
			var ss = $(".colorId_"+param.searchId.replace(" ",""));
			ss.addClass("selectColor");
		}).catch(function(err) {
			console.log(err);
		});
	}
};
